﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using SalesSys.BLL;
using SalesSys.DAL.Models;
using SalesSys.WebCore;

namespace SalesSys.Site.Controllers.Api
{
    /// <summary>
    /// 非标优惠
    /// </summary>
    [RoutePrefix("api/NonstandardDiscount")]
    public class NonstandardDiscountController : BaseApiController
    {
        ProviderBLL depBLL = BllFactory.GetBll<ProviderBLL>();

        /// <summary>
        /// 获取供应商非标优惠
        /// </summary>
        /// <param name="depotId">供应商编号</param>
        /// <returns></returns>
        public KeyValuePair<Supplier, decimal>[] Get(Guid depotId)
        {
            var depotDis = depBLL.GetNonstandardDiscount(depotId);
            return depotDis.ToArray();
        }

        /// <summary>
        /// 修改供应商钢厂非标优惠
        /// </summary>
        /// <param name="depotId">供应商</param>
        /// <param name="supId">钢厂</param>
        /// <param name="discount">非标优惠</param>
        public void Put(Guid depotId, Guid supId, decimal discount)
        {
            var depot = depBLL.Details(depotId);
            var dic = depot.GetNonstandardDiscountDic();
            if (!dic.ContainsKey(supId))
            {
                throw new ApiArgException("修改项不能找到");
            }
            dic[supId] = discount;
            depot.SetNonstandardDiscountCase(dic);
            depBLL.Edit(depot);
        }

        /// <summary>
        /// 删除供应商钢厂非标优惠
        /// </summary>
        /// <param name="depotId">供应商</param>
        /// <param name="supId">钢厂</param>
        public void Delete(Guid depotId, Guid supId)
        {
            var depot = depBLL.Details(depotId);
            var dic = depot.GetNonstandardDiscountDic();
            if (!dic.ContainsKey(supId))
            {
                throw new ApiArgException("修改项不能找到");
            }
            dic.Remove(supId);
            if (dic.Count == 0)
            {
                depot.NonstandardDiscountCase = null;
            }
            depBLL.Edit(depot);

        }

        /// <summary>
        /// 新增供应商钢厂非标优惠
        /// </summary>
        /// <param name="depotId">供应商</param>
        /// <param name="supId">钢厂</param>
        /// <param name="discount">非标优惠</param>
        public void Post(Guid depotId, Guid supId, decimal discount)
        {
            var depot = depBLL.Details(depotId);
            var dic = depot.GetNonstandardDiscountDic();
            if (dic.ContainsKey(supId))
            {
                throw new ApiArgException("已存在的优惠项");
            }
            dic.Add(supId, discount);
            depot.SetNonstandardDiscountCase(dic);
            depBLL.Edit(depot);
        }

        /// <summary>
        /// 修改供应商钢厂非标优惠
        /// </summary>
        /// <param name="depotId">供应商</param>
        /// <param name="dic">钢厂优惠数组</param>
        [Route("Edit")]
        public void PutEdit(Guid depotId, KeyValuePair<Supplier, decimal>[] dic)
        {
            var depot = depBLL.Details(depotId);
            depot.SetNonstandardDiscountCase(dic.ToDictionary(p => p.Key.Id, p => p.Value));
            depBLL.Edit(depot);
        }
    }
}
